package com.epam.qanda.web.servlet.logged;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.epam.qanda.db.model.Question;
import com.epam.qanda.db.model.User;
import com.epam.qanda.db.model.UserQuestionLike;
import com.epam.qanda.db.service.QuestionService;
import com.epam.qanda.db.service.UserQuestionLikeService;
import com.epam.qanda.db.service.UserService;

public class LikeQuestionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String id = request.getParameter("questionId");
		String url = request.getServletPath();
		Integer questionId = Integer.parseInt(id);
		Question question = new QuestionService().read(questionId);
		User user = (User) request.getSession().getAttribute("user");
		User userAnswer = new UserService().read(question.getUserId());
		if (user != null) {
			if (!user.isStatus()) {
				if (!user.getId().equals(question.getUserId())) {

					UserQuestionLike userQuestionLikes = new UserQuestionLikeService()
							.getUserQuestion(user.getId(), questionId);
					if (userQuestionLikes == null) {
						userQuestionLikes = new UserQuestionLike();
						userQuestionLikes.setUserId(user.getId());
						userQuestionLikes.setQuestionId(questionId);
						if (url.equals("/increaseLikeQuestion")) {
							question.setLikes(question.getLikes() + 1);
							userQuestionLikes.setDirection(true);
							userAnswer.setRating(userAnswer.getRating() + 1);
							if (userAnswer.getRating() > (-20)) {
								userAnswer.setStatus(false);
							}
							new UserService().update(userAnswer);
						}
						if (url.equals("/decreaseLikeQuestion")) {
							question.setLikes(question.getLikes() - 1);
							userQuestionLikes.setDirection(false);
							userAnswer.setRating(userAnswer.getRating() - 1);
							if (userAnswer.getRating() <= (-20)) {
								userAnswer.setStatus(true);
							}
							new UserService().update(userAnswer);
						}
						new QuestionService().updateLikes(question);
						new UserQuestionLikeService().create(userQuestionLikes);
						response.getWriter().println(question.getLikes());
					} else {

						if (userQuestionLikes.getDirection() == true) {

							if (url.equals("/decreaseLikeQuestion")) {
								new UserQuestionLikeService().delete(userQuestionLikes.getId());
								question.setLikes(question.getLikes() - 1);
								new QuestionService().updateLikes(question);
								userAnswer.setRating(userAnswer.getRating() - 1);
								if (userAnswer.getRating() <= (-20)) {
									userAnswer.setStatus(true);
								}
								new UserService().update(userAnswer);
								response.getWriter().println(question.getLikes());
							} else {
								response.getWriter().println("You've already liked this question!");
							}
						} else {
							if (url.equals("/increaseLikeQuestion")) {
								new UserQuestionLikeService().delete(userQuestionLikes.getId());
								question.setLikes(question.getLikes() + 1);
								new QuestionService().updateLikes(question);
								userAnswer.setRating(userAnswer.getRating() + 1);
								if (userAnswer.getRating() > (-20)) {
									userAnswer.setStatus(false);
								}
								new UserService().update(userAnswer);
								response.getWriter().println(question.getLikes());

							} else {
								response.getWriter().println(
										"You've already disliked this question!");
							}
						}
					}

				} else {
					response.getWriter().println("You can't like or dislike your own question!");
				}
			} else {
				response.getWriter().println(
						"You can't like or dislake question, because your account is blocked!");
			}

		} else {
			response.getWriter().println("");
		}
	}
}
